package com.xinongbest.spring_sign_in.entity.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "返回结果实体类", description = "结果实体类")
public class ServerResponse implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;

    public ServerResponse() {

    }

    public ServerResponse(ResultCodeEnum codeEnum, Object data) {
        this.code = codeEnum.code();
        this.message = codeEnum.message();
        this.data = data;
    }

    private void setServerStatus(ResultCodeEnum codeEnum) {
        this.code = codeEnum.code();
        this.message = codeEnum.message();
    }

    public static ServerResponse success() {
        ServerResponse response = new ServerResponse();
        response.setServerStatus(ResultCodeEnum.SUCCESS);
        return response;
    }

    public static ServerResponse success(Object data) {
        ServerResponse response = new ServerResponse();
        response.setServerStatus(ResultCodeEnum.SUCCESS);
        response.setData(data);
        return response;
    }

    public static ServerResponse fail(Integer code, String message) {
        ServerResponse response = new ServerResponse();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public static ServerResponse fail(ResultCodeEnum codeEnum) {
        ServerResponse response = new ServerResponse();
        response.setServerStatus(codeEnum);
        return response;
    }
}
